<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of pro
 *
 * @author Gidaff_01
 */
class Pro  extends Admin_Controller{
    function Pro(){
        parent::Admin_Controller();
        $this->load->model('Pro_m');
    }
    
    function index() {
        $this->liste_pro();
    }

    function liste_pro() {
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 10;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/pro/liste_pro/' . $per_page . '/';
        $config['uri_segment'] = 4;
        $config['per_page'] = '10';
        $config['total_rows'] = $this->Pro_m->count_all_pro();

        if ($config['total_rows'] == $off_set && $off_set > 0) {
            $off_set = $off_set - $per_page;
        }

        $this->_data['pro']=$this->Pro_m->get_all_pro($per_page,$off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['page_title'] = "Liste pro";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_data['total'] = $this->db->count_all('pro');
        $this->display_view('pro/liste_pro');
    }

    function delete_pro($id,$per_page,$off_set){
        is_admin();
        $this->Pro_m->delete_pro($id);
       redirect(site_url('pro/liste_pro/' . $per_page . '/' . $off_set));
    }

    function update_pro($id, $per_page, $off_set){
        is_admin();
        $config = array(
            array(
                'field' => 'nom',
                'label' => 'nom ',
                'rules' => 'required'
            ),
            array(
                'field' => 'email',
                'label' => 'email ',
                'rules' => 'required|valid_email'
            ),
            array(
                'field' => 'tel',
                'label' => 'tel ',
                'rules' => 'required'
            ),
            array(
                'field' => 'adresse',
                'label' => 'adresse ',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        $this->_data['query'] = $this->Pro_m->get_pro_by_id($id);
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        if($this->input->post('submit')){
                if($this->form_validation->run()){
                    $data = array(
                        'nom' => $this->input->post('nom'),
                        'email' => $this->input->post('email'),
                        'tel' => $this->input->post('tel'),
                        'adresse' => $this->input->post('adresse')
                       );
                    $this->Pro_m->update_pro($data, $id);
                    redirect(site_url('pro/liste_pro/' . $per_page . '/' . $off_set));
                }else{
                    $this->display_view('pro/update_pro');
                }
        }else{
            $this->display_view('pro/update_pro');
        }
    }

    function add_pro(){
        is_admin();
        $config = array(
            array(
                'field' => 'nom',
                'label' => 'nom ',
                'rules' => 'required'
            ),
            array(
                'field' => 'ident',
                'label' => 'ident ',
                'rules' => 'required'
            ),
            array(
                'field' => 'email',
                'label' => 'email ',
                'rules' => 'required|valid_email'
            ),
            array(
                'field' => 'tel',
                'label' => 'tel ',
                'rules' => 'required'
            ),
            array(
                'field' => 'adresse',
                'label' => 'adresse ',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        if($this->input->post('submit')){
                if($this->form_validation->run()){
                    $data = array(
                        'nom' => $this->input->post('nom'),
                        'ident' => $this->input->post('ident'),
                        'email' => $this->input->post('email'),
                        'tel' => $this->input->post('tel'),
                        'adresse' => $this->input->post('adresse'),
                        'pass' => md5('1234567')
                       );
                    $this->Pro_m->add_pro($data);
                    redirect(site_url('pro/liste_pro'));
                }else{
                    $this->display_view('pro/add_pro');
                }
        }else{
            $this->display_view('pro/add_pro');
        }
    }
}
?>
